import time
from datetime import datetime
from tools.myredis import r
from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
from tools.myjwt import myjwt
import json
from dkeep.settings import SECRET_KEY

class PermitionMiddleware(MiddlewareMixin):
    def process_request(self,request):
        #定义白名单
        wlist = ['/app/user/login_user/']
        #获取token
        url = request.path
        print(url)
        if url not in wlist:
            #解析验证
            try:
                token = request.headers.get('Authorization')
                print(token)
                data = myjwt.jwt_decode(token)
                print(data)
                if int(time.time()) >  int(data['exp']):
                    return JsonResponse({"code":401})
                # plist = r.get_str('user1promition')
                # if plist:
                #     plist = json.loads(plist)
                #     if url not in plist:
                #         return JsonResponse({"code":402})
                # else:
                #      return JsonResponse({"code":403})
            except:
                return JsonResponse({"code":402})